<template>
  <view>
    <!--顶部自定义组件搜索框-->
    <view class="search-box">
      <my-search @click='gotoSerach'></my-search>
    </view>

    <!--轮播图区域-->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item,index) in swiperList" :key="index">
        <navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id">
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>

    <!-- 中间分类区域-->
    <view class="nav">
      <view class="navItem" v-for="(item,index) in navList" :key="index" @click="navClickHandler(item)">
        <image :src="item.image_src"></image>
      </view>
    </view>

    <!--底部楼层区域-->
    <view class="floor">
      <view class="floorItem" v-for="(item,index) in floorList" :key="index">
        <!-- 楼层标题 -->
        <image :src="item.floor_title.image_src" class="floor-title"></image>
        <!-- 楼层图片-->
        <view class="floor-image">
          <navigator class="left-image" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src" :style="{width: item.product_list[0].image_width + 'rpx'}"
              mode='widthFix'></image>
          </navigator>
          <view class="right-image">
            <navigator v-for="(item2,index2) in item.product_list" :key="index2" v-if="index2!=0" :url="item2.url">
              <image :src="item2.image_src" :style="{width: item2.image_width + 'rpx'}" mode='widthFix'></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>

  </view>
</template>


<script>
  import badgeMix from '@/mixins/tabbar-badge.js'
  export default {
    mixins: [badgeMix],
    data() {
      return {
        //存储轮播图的图片
        swiperList: [],
        //存储中间分类区域数据
        navList: [],
        //存储底部楼层图数据
        floorList: []
      };
    },
    //首页一加载就调用这个（onLoad）生命周期方法中的事件函数渲染首页！！！
    onLoad() {
      //获取服务器中的轮播图数据
      this.getSwiperList()
      //获取分类数据
      this.getNavList()
      //获取底部楼层数据
      this.getFloorList()
    },
    methods: {
      async getSwiperList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/swiperdata')
        if (res.meta.status != 200) return uni.$showMsg()
        this.swiperList = res.message
      },

      async getNavList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/catitems')
        if (res.meta.status != 200) return uni.$showMsg()
        this.navList = res.message
      },

      navClickHandler(item) {
        if (item.name === '分类') {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },

      async getFloorList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/floordata')
        if (res.meta.status != 200) return uni.$showMsg()
        console.log(res)
        //对数据进行处理
        res.message.forEach(floor => {
          floor.product_list.forEach(prod => {
            prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
          })
        })
        this.floorList = res.message
      },

      gotoSerach() {
        uni.navigateTo({
          url: "/subpkg/serach/serach"
        })
      }
    }
  }
</script>

<style lang="scss">
  .search-box {
    position: sticky;
    z-index: 999999;
    top: 0;
  }

  swiper {
    height: 330rpx;

    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }
  }

  .nav {
    display: flex;
    justify-content: space-around;
    margin-top: 20rpx;

    .navItem {
      image {
        width: 128rpx;
        height: 140rpx;
      }
    }
  }

  .floor {
    margin: 20rpx 0;

    .floor-title {
      margin: 20rpx 0;
      height: 60rpx;
      width: 100%;
      display: flex;
    }

    .floor-image {
      padding-left: 10rpx;
      display: flex;

      .right-image {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-around;
      }
    }
  }
</style>
